﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneEditor
{
    public class Document
    {
        /// <summary>
        /// Do not create Document objects directly. Use the DocumentManager.
        /// </summary>
        internal Document( SceneModel model, View.IDocumentView view )
        {
            this.Model = model;
            this.View = view;
        }

        public readonly SceneModel Model;
        public readonly View.IDocumentView View;
    }
     
    public class DocumentManager
    {
        readonly App _app;
        Document _activeDocument;
        List<Document> _openDocuments = new List<Document>();

        public DocumentManager( App app )
        {
            _app = app;
        }

        /// <summary>
        /// This event is raised when the active document has changed;
        /// </summary>
        public event Action<DocumentManager, Document> ActiveDocumentChanged;

        /// <summary>
        /// Returns the currently active document. May be null.
        /// </summary>
        public Document ActiveDocument
        {
            get { return _activeDocument; }
            set
            {
                Debug.Assert( _openDocuments.Contains( value ) || value == null );

                if ( value != _activeDocument )
                {
                    _activeDocument = value;

                    if ( value != null )
                    {
                        _app.View.MainForm.ActiveDocumentView = _activeDocument.View;
                    }

                    if ( this.ActiveDocumentChanged != null )
                    {
                        this.ActiveDocumentChanged( this, _activeDocument );
                    }
                }
            }
        }

        /// <summary>
        /// An enumeration of all open documents.
        /// </summary>
        public IEnumerable<Document> OpenDocuments
        {
            get { return _openDocuments; }
        }

        /// <summary>
        /// Creates a new empty document.
        /// </summary>
        public Document CreateNewDocument()
        {
            var model = SceneModel.CreateNewSceneModel();


            var controller = new DocumentController( _app );
            var view = _app.View.MainForm.CreateDocumentView( controller, model );
            var document = new Document( model, view );

            _openDocuments.Add( document );

            this.ActiveDocument = document;

            return document;
        }

        /// <summary>
        /// Loads the document with the given resource name.
        /// </summary>
        /// <remarks>
        /// If the document is already loaded then the existing document will be focused.
        /// </remarks>
        public Document LoadDocument( String resourceName )
        {
            var document =
                _openDocuments.Find( doc => doc.Model.NameOfAssociatedResource == resourceName );

            if ( document != null )
            {
                return document;
            }

            var model = SceneModel.CreateSceneModelFromResource( resourceName );

            var controller = new DocumentController( _app );
            var view = _app.View.MainForm.CreateDocumentView( controller, model );

            document = new Document( model, view );
            _openDocuments.Add( document );

            this.ActiveDocument = document;

            return document;
        }

        /// <summary>
        /// Closes a document.
        /// </summary>
        /// <param name="document">Must be contained in OpenDocuments.</param>
        public void CloseDocument( Document document )
        {
            Debug.Assert( _openDocuments.Contains( document ) );

            _openDocuments.Remove( document );

            document.Model.Dispose();
            _app.View.MainForm.CloseDocumentView( document.View );

            _setActiveDocumentFromView();
        }

        void _setActiveDocumentFromView()
        {
            View.IDocumentView activeView = _app.View.MainForm.ActiveDocumentView;

            if ( activeView != null )
            {
                this.ActiveDocument = _openDocuments.Single( doc => doc.View == activeView );
            }
            else
            {
                this.ActiveDocument = null;
            }
        }
    }
}
